<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span>扣费设置</span><div class="move_title"></div><?php include "../public/sub.php";?></div></div>
    <div class="main-container">

        <div class="block-generic">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>费用设置</span><span id="kouhelpss" style="cursor: pointer;font-size: 18px"><i class="layui-icon layui-icon-help"></i></span></div>
                        <div>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_copy_row">复制行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row">插入行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th><text>*</text>费用项</th>
                            <th><text>*</text>计算方式</th>
                            <th><text>*</text>计算值</th>
                            <th>开始计算月份</th>
                            <th>结束计算月份</th>
                            <th>备注</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list"></tbody>
                    </table>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>
        </div>
    </div>
</form>
<script>
    var param_id = getQueryVariable('id') || 0;

    var pub_feiname;
    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();



        facade.ajax({type: 'get', url: ask_contract_nei_kou, data:{id:param_id}}).done(function(res){
            if (res.code === 2000) {
                var data = res.data.data;

                pub_feiname = res.data.feiname;
                var feitype = res.data.feitype;

                var listAddStr = '';
                listAddStr += '<tr>';
                listAddStr += '<td style="width: 45px">1</td>';
                listAddStr += '<td>';
                listAddStr += '<div class="feiname"></div>';
                listAddStr += '</td>';
                listAddStr += '<td>';
                listAddStr += '<select class="layui-select feitype" lay-filter="feitype">';
                listAddStr += '<option value="">请选择</option>';
                for (var key in feitype) {
                    listAddStr += '<option value="'+key+'">'+feitype[key]+'</option>';
                }
                listAddStr += '</select>';
                listAddStr += '</td>';
                listAddStr += '<td><input type="number" class="layui-input feival"></td>';
                listAddStr += '<td><input type="text" class="layui-input startdt bdate_ym" readonly></td>';
                listAddStr += '<td><input type="text" class="layui-input enddt bdate_ym" readonly></td>';
                listAddStr += '<td><input type="text" class="layui-input explain"></td>';
                listAddStr += '</tr>';
                short_table_opt_func(listAddStr, 1, {name:'feiname', data:res.data.feiname, value:''});



                var list = res.data.list;
                if(list.length > 0){
                    for (var key in list){
                        var str = '';
                        var nums = parseInt(key)+1;
                        str += '<tr>';
                        str += '<td style="width: 45px">'+nums+'</td>';
                        str += '<td><input type="hidden" class="id" value="'+list[key].id+'" readonly>';
                        str += '<div class="feiname feiname'+nums+'" key="'+nums+'"></div>';
                        str += '</td>';
                        str += '<td>';
                        str += '<select class="layui-select feitype" lay-filter="feitype">';
                        str += '<option value="">请选择</option>';
                        for (var mmm in feitype) {
                            if(list[key].feitype == mmm){
                                str += '<option value="'+mmm+'" selected>'+feitype[mmm]+'</option>';
                            }else{
                                str += '<option value="'+mmm+'">'+feitype[mmm]+'</option>';
                            }
                        }
                        str += '</select>';
                        str += '</td>';
                        str += '<td><input type="number" class="layui-input feival" value="'+list[key].feival+'"></td>';
                        if(list[key].feitype == 2){
                            str += '<td><input type="text" class="layui-input startdt bdate_ym" value="'+list[key].startdt+'" readonly></td>';
                            str += '<td><input type="text" class="layui-input enddt bdate_ym" value="'+list[key].enddt+'" readonly></td>';
                        }else{
                            str += '<td><input type="text" class="layui-input startdt" readonly></td>';
                            str += '<td><input type="text" class="layui-input enddt" readonly></td>';
                        }
                        str += '<td><input type="text" class="layui-input explain" value="'+list[key].explain+'"></td>';
                        str += '</tr>';
                        $("#list_list").append(str);
                        xmselect_list_dan('feiname'+nums, res.data.feiname, list[key].feiname);
                    }
                }


                render_date();
                initParseMoney();
                render_from();

            }
        });


        form.on('select(feitype)', function(data){
            if(data.value == 2){
                $(this).parents('tr').find('.startdt').addClass('bdate_ym');
                $(this).parents('tr').find('.enddt').addClass('bdate_ym');
            }else{
                $(this).parents('tr').find('.startdt').parent('td').html('<input type="text" class="layui-input startdt " readonly>');
                $(this).parents('tr').find('.enddt').parent('td').html('<input type="text" class="layui-input enddt " readonly>');
            }
            render_from();
            render_date();
        });

        form.on('submit(add)', function(data){
            var list = [''];
            try {
                $('#list_list tr').each(function (i) {
                    var id = $(this).find('.id').val();
                    var feiname = $(this).find('.feiname').find('input').val();
                    var feitype = $(this).find('.feitype').val();
                    var feival = $(this).find('.feival').val();
                    var startdt = $(this).find('.startdt').val();
                    var enddt = $(this).find('.enddt').val();
                    var explain = $(this).find('.explain').val();

                    var index = i + 1;

                    if(!feiname){
                        throw new Error('第'+index+'行，请选择费用项');
                    }
                    if(!feitype){
                        throw new Error('第'+index+'行，请选择计算方式');
                    }
                    if(!feitype){
                        throw new Error('第'+index+'行，请输入计算值');
                    }
                    if(feitype == 2){
                        if(!startdt){
                            throw new Error('第'+index+'行，请选择开始计算月份');
                        }
                    }else{
                        startdt = '';
                        enddt = '';
                    }

                    list.push({
                        id:id,
                        feiname:feiname,
                        feitype:feitype,
                        feival:feival,
                        startdt:startdt,
                        enddt:enddt,
                        explain:explain,
                    });
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();
            data.field.list = list;


            data.field.id = param_id;
            data = parseMoney(data);
            facade.ajax({url: ask_contract_nei_kou, data:data.field}).done(function(res){
                if (res.code === 2000) {
                    dataPostTimeOut();
                }
            });
            return false;
        });

    });


    var curTipIndex = '';
    $("#kouhelpss").on("mouseenter", function () {
        var content = '<div style="padding: 5px; font-size: 12px; color: #FFFFFF;">' +
            '<div>·按固定金额：填写的「计算值」将直接作为该费项的应收取值</div>'+
            '<div>·按月固定金额：填写的「计算值」将每月进行累加（需填写开始和结束月份）</div>'+
            '<div>·按合同金额比例：填写的「计算值」将作为计算比例，与施工合同中的「合同金额」的金额相乘得出应收</div>'+
            '<div>·按工程收入比例：填写的「计算值」将作为计算比例，与收款登记中的「工程款」的金额相乘得出应收</div>'+
            '<div>·按开票金额比例：填写的「计算值」将作为计算比例，与开票登记中的的含税金额相乘得出应收</div>'+
            '<div>·按用证清单比例：适用于证书费，填写的「计算值」将作为计算比例，与用证清单的使用费合计金额相乘得出应收</div>'+
            "</div>";
        curTipIndex = layer.tips(content,this, {time: -1, maxWidth: 600, tips: [3, "#2272eb"],});
    }).on("mouseleave", function () {
        layer.close(curTipIndex);
    });

</script>
</body>
</html>
